package com.example.demo.service.impl;

import com.example.common.utils.TrimSpaceUtil;
import com.example.demo.domain.*;
import com.example.demo.domain.DTO.*;
import com.example.demo.service.SpotInfoService;
import com.example.demo.mapper.SpotInfoMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author z'z'w
 * @description 针对表【SPOT_INFO】的数据库操作Service实现
 * @createDate 2023-05-12 08:52:47
 */
@Service
public class SpotInfoServiceImpl implements SpotInfoService {

    @Autowired
    private SpotInfoMapper spotInfoMapper;

    public List<SpotInfo> selectAll() {
        List<SpotInfo> spotInfos = spotInfoMapper.selectAll();
        List<SpotInfo> result = new ArrayList<>();
        for (SpotInfo spotInfo : spotInfos) {
            SpotInfo o = (SpotInfo) TrimSpaceUtil.replaceBlankSpace(spotInfo);
            result.add(o);
        }
        return result;
    }

    @Override
    public List<SpotHotInfo> selectHotInfoTop10() {
        return spotInfoMapper.selectHotInfoTop10();
    }

    @Override
    public List<SpotGradeInfo> selectGradeTop10() {
        return spotInfoMapper.selectGradeTop10();
    }

    /**
     * 推荐值数前10的景区
     */
    @Override
    public List<SpotInfo> selectRatingTop10() {
        List<SpotInfo> spotInfos = spotInfoMapper.selectRatingTop10();
        List<SpotInfo> result = new ArrayList<>();
        for (SpotInfo spotInfo : spotInfos) {
            //去空格字符流
            SpotInfo o = (SpotInfo) TrimSpaceUtil.replaceBlankSpace(spotInfo);
            result.add(o);
        }
        return result;
    }

    @Override
    public SpotInfo selectMaxSum() {
        return spotInfoMapper.selectMaxSum();
    }

    /**
     * 根据景点ID获取景点信息
     *
     * @param spotId 景点ID
     * @return 景点信息
     */
    @Override
    public SpotInfo selectSpotInfoById(int spotId) {
        return spotInfoMapper.selectSpotInfoById(spotId);
    }

    //季节总
    @Override
    public HashMap<String, HashMap<String, Object>> selectSeasonSpotInfo() {
        List<SeasonSpotInfo> seasonSpotInfos = spotInfoMapper.selectSeasonSpotInfo();
        HashMap<String, HashMap<String, Object>> seasonSpotInfosMap = new HashMap<>();
        for (SeasonSpotInfo seasonSpotInfo : seasonSpotInfos) {
            //先对对象取值判断
            String season = seasonSpotInfo.getSeason();
            if (season == null || season.equals("")) continue;
            String spotName = seasonSpotInfo.getSpotName();
            if (spotName == null || spotName.equals("")) continue;
            Object sumInfo = seasonSpotInfo.getSumInfo();
            if (sumInfo == null || sumInfo.equals("")) continue;
            //开始封装
            if (seasonSpotInfosMap.containsKey(season)) {
                HashMap<String, Object> seasonPoint = seasonSpotInfosMap.get(season);
                //判断这个景点是不是在该季节map当中,如果不是则添加
                if (!seasonPoint.containsKey(spotName)) {
                    seasonPoint.put(spotName, sumInfo);
                }
            } else {
                //如果不在则创建一个季节的key 并赋予其景点及评论sum
                HashMap<String, Object> spotInfoMap = new HashMap<>();
                spotInfoMap.put(spotName, sumInfo);
                seasonSpotInfosMap.put(season, spotInfoMap);
            }
        }
        return seasonSpotInfosMap;
    }

    //    季节前十
    @Override
    public HashMap<String, List<SeasonSpot10Info>> selectSeasonSpot10Info() {
        List<SeasonSpot10Info> seasonSpot10Infos = spotInfoMapper.selectSeasonSpot10Info();
        HashMap<String, List<SeasonSpot10Info>> result = new HashMap<>();
        for (SeasonSpot10Info seasonSpot10Info : seasonSpot10Infos) {
            //已存在
            String season = seasonSpot10Info.getSeason();
            if (result.containsKey(season)) {
                result.get(season).add(seasonSpot10Info);
            } else {
                //不存在的话就添加
                List<SeasonSpot10Info> list = new ArrayList<>();
                list.add(seasonSpot10Info);
                result.put(season, list);
            }
        }
        return result;
    }

    /**
     * 景区数量
     */
    @Override
    public List<SpotLevelCnt> selectSpotLevelCnt() {
        return spotInfoMapper.selectSpotLevelCnt();
    }

    @Override
    public List<SpotSearch> selectSpotOtherInfo() {
        List<SpotSearch> spotSearches = spotInfoMapper.selectSpotOtherInfo();
        List<SpotSearch> result = new ArrayList<>();
        //去除数据中不干净的数据
        for (SpotSearch spotSearch : spotSearches) {
            //去除
            result.add((SpotSearch) TrimSpaceUtil.replaceBlankSpace(spotSearch));
        }
        return spotSearches;
    }

    /**
     * 各景点评论数
     *
     * @return
     */
    @Override
    public List<SpotComCntInfo> selectSpotComCntInfo() {
        List<SpotComCntInfo> spotComCntInfos = spotInfoMapper.selectSpotComCntInfo();
        //去除数据中不干净的数据
        for (SpotComCntInfo spotSumComInfo : spotComCntInfos) {
            //景区名
            String spotName = spotSumComInfo.getSpotName();
            String replaceSpotName = spotName.replace("景区", "").replace("A", "").replaceAll("\\s*|\t|\r|\n", "");
            spotSumComInfo.setSpotName(replaceSpotName);
        }
        return spotComCntInfos;
    }

    /**
     * 返回景点的五个指标
     *
     * @return 景点五个指标
     */
    @Override
    public SpotFiveInfo selectSpotFiveInfo(int spotId) {
        return spotInfoMapper.selectSpotFiveInfo(spotId);
    }

    /**
     * 返回集体景点的平均五个指标
     *
     * @return 集体景点五个指标
     */
    @Override
    public SpotAvgFiveInfo selectAllSpotAvgFiveInfo() {
        return (SpotAvgFiveInfo) TrimSpaceUtil.replaceBlankSpace(spotInfoMapper.selectAllSpotAvgFiveInfo());
    }

    @Override
    public HashMap<String, List<SpotMonthInfo>> selectSpotMonthInfo() {
        List<SpotMonthInfo> spotMonthInfos = spotInfoMapper.selectSpotMonthInfo();
        HashMap<String, List<SpotMonthInfo>> spotMonthInfoMap = new HashMap<>();
        for (SpotMonthInfo spotMonthInfo : spotMonthInfos) {
            String comTime = spotMonthInfo.getComTime() + "";
            //判断这个月份的数据是否存在
            //存在
            if (spotMonthInfoMap.containsKey(comTime)) {
                spotMonthInfoMap.get(comTime).add(spotMonthInfo);
            } else {
                List<SpotMonthInfo> spotMonthInfoList = new ArrayList<>();
                spotMonthInfoList.add(spotMonthInfo);
                spotMonthInfoMap.put(comTime, spotMonthInfoList);
            }
        }
        return spotMonthInfoMap;
    }


    /**
     * 某个景区来自各个省的游客数
     */
    @Override
    public SpotProvinceDto selectSpotProvincePeopleNum(int spotId) {
        List<SpotProvincePeopleNum> spotProvincePeopleNums = spotInfoMapper.selectSpotProvincePeopleNum(spotId);
        SpotProvinceDto spotProvinceDto = new SpotProvinceDto();
        //跳过封装景区名
        boolean flag = true;
        HashMap<String, Object> spotProvincePeopleNumMap = new HashMap<>();
        //封装
        for (SpotProvincePeopleNum spotProvincePeopleNum : spotProvincePeopleNums) {
            if (flag) {
                //景区名
                String spotName = spotProvincePeopleNum.getSpotName();
                spotProvinceDto.setSpotName(spotName);
                flag = false;
            }

            //地区
            String province = spotProvincePeopleNum.getProvince();
            //人数
            Object numPerson = spotProvincePeopleNum.getNumPerson();
            //纬度
            Object latitude = spotProvincePeopleNum.getLatitude();
            //经度
            Object longitude = spotProvincePeopleNum.getLongitude();
            //封装 地区:人数
            spotProvincePeopleNumMap.put(province, new PeopleNum(numPerson, latitude, longitude));
        }
        //封装结果
        spotProvinceDto.setSpotProvincePeopleNumMap(spotProvincePeopleNumMap);
        return spotProvinceDto;
    }

    /**
     * rating前十景区的各月评论数
     *
     * @return
     */
    @Override
    public HashMap<String, HashMap<String, Object>> selectRatingTop10CntInfo() {
        //结果集
        HashMap<String, HashMap<String, Object>> result = new HashMap<>();
        //初始数据
        List<RatingTop10CntInfo> ratingTop10CntInfos = spotInfoMapper.selectRatingTop10CntInfo();
        //封装
        for (RatingTop10CntInfo ratingTop10CntInfo : ratingTop10CntInfos) {
            //景点名称
            String spotName = ratingTop10CntInfo.getSpotName();
            //如果景点key已经存在
            if (result.containsKey(spotName)) {
                //获取key值对应的map
                HashMap<String, Object> monthComHashMap = result.get(spotName);
                //每一个月份都不会重复，所以无需判断月份
                monthComHashMap.put(ratingTop10CntInfo.getMonthNum(), ratingTop10CntInfo.getComCnt());
            } else {
                HashMap<String, Object> monthComHashMap = new HashMap<>();
                //每一个月份都不会重复，所以无需判断月份
                monthComHashMap.put(ratingTop10CntInfo.getMonthNum(), ratingTop10CntInfo.getComCnt());
                //添加这个景点的相应值
                result.put(spotName, monthComHashMap);
            }
        }
        return result;
    }

    /**
     * rating前十的景区的 各省份来访人数
     */
    @Override
    public HashMap<String, HashMap<String, Object>> selectRatingTop10ProvinceInfo() {
        //结果集
        HashMap<String, HashMap<String, Object>> result = new HashMap<>();
        //初始数据
        List<RatingTop10ProvinceInfo> ratingTop10ProvinceInfos = spotInfoMapper.selectRatingTop10ProvinceInfo();
        //封装
        for (RatingTop10ProvinceInfo ratingTop10ProvinceInfo : ratingTop10ProvinceInfos) {
            //景点名称
            String spotName = ratingTop10ProvinceInfo.getSpotName();
            //如果景点key已经存在
            if (result.containsKey(spotName)) {
                //获取key值对应的map
                HashMap<String, Object> monthComHashMap = result.get(spotName);
                //每一个月份都不会重复，所以无需判断月份
                monthComHashMap.put(ratingTop10ProvinceInfo.getProvince(), ratingTop10ProvinceInfo.getProvinceCnt());
            } else {
                HashMap<String, Object> monthComHashMap = new HashMap<>();
                //每一个月份都不会重复，所以无需判断月份
                monthComHashMap.put(ratingTop10ProvinceInfo.getProvince(), ratingTop10ProvinceInfo.getProvinceCnt());
                //添加这个景点的相应值
                result.put(spotName, monthComHashMap);
            }
        }
        return result;

    }


}




